package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Lff
 * @date 2022/3/4
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService service;

    /**
     * 查询购物车里的信息
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        List<ShoppingCart> pages = service.selectByList();
        return R.success(pages);
    }

    /**
     * 添加购物车信息
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart = service.addShoppingCart(shoppingCart);
        return R.success(shoppingCart);
    }

    /**
     * 减少购物车商品数量
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> delShoppingCart(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart = service.delShoppingCart(shoppingCart);
        return R.success(shoppingCart);
    }

    /**
     * 清除用户购物车信息
     */
    @DeleteMapping("/clean")
    public R<String> clear() {
        service.clearShoppingCart();
        return R.success("清空购物车成功！");
    }
}
